//
//  LLRowPickView.m
//  JoySpell
//
//  Created by linl on 2018/3/26.
//  Copyright © 2018年 xkdao. All rights reserved.
//

#import "M1RowPickView.h"
#import "M1PickerTF.h"

#import "M1RowPicker.h"
//#import "UIImageView+WebCache.h"
//#import "LLImagePicker.h"

#define m1_NULLString(string) ((![string isKindOfClass:[NSString class]])||[string isEqualToString:@""] || (string == nil) || [string isEqualToString:@""] || [string isKindOfClass:[NSNull class]]||[[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0)

NSString * const m1_RowPickerKeyName = @"kRowPickKeyName";
NSString * const m1_RowPickerKeyType = @"kRowPickKeyType";
NSString * const m1_RowPickerKeyValue = @"kRowPickKeyValue";
NSString * const m1_RowPickerKeyData = @"kRowPickKeyData";
NSString * const m1_RowPickerKeyHoldText = @"kRowPickKeyHoldText";
NSString * const m1_RowPickerKeyDefaultSelect = @"kRowPickKeyDefaultSelect";
NSString * const m1_RowPickerKeyKeyHasLine = @"kRowPickKeyHasLine";
NSString * const m1_RowPickerKeyHasArrow = @"kRowPickKeyHasArrow";
NSString * const m1_RowPickerKeyHeight = @"kRowPickKeyHeight";
NSString * const m1_RowPickerKeyPickNum = @"kRowPickKeyPickNum";
NSString * const m1_RowPickerKeyIsSelection = @"kRowPickKeyIsSelection";
NSString * const m1_RowPickerKeyEditable = @"kRowPickKeyEditable";
NSString * const m1_RowPickerKeyShowAttrs = @"kRowPickKeyShowAttrs";
NSString * const m1_RowPickerKeyHoldAttrs = @"kRowPickKeyHoldAttrs";

static NSString *kLayerTag = @"layerTag";

@interface M1RowPickView () <UITextFieldDelegate>
{
    CGFloat m1_pickerHeight;
    CGFloat m1_pickerLeft;
    CGFloat m1_pickerRight;
    NSInteger m1_pickerNum;
    
    NSString *m1_pickerName;
    EnumPickType m1_pickerType;
    NSString *m1_pickerValue;
    NSArray *m1_pickerData;
    NSString *m1_pickerHold;
    NSString *m1_pickerDefault;
    BOOL m1_pickerHasLine;
    BOOL m1_pickerHasArrow;
    BOOL m1_pickerSelect;
    BOOL m1_pickerEditable;
    
    NSDictionary<NSAttributedStringKey, id> *m1_pickerShowText;
    NSDictionary<NSAttributedStringKey, id> *m1_pickerHoldText;
    
    BOOL m1_isNew;
}

@property (nonatomic) UIView *m1_RowView;

@property (nonatomic) M1PickerTF *m1_pickerTextField;

@property (nonatomic) UIImageView *m1_imgHeader;

@property (nonatomic) UIImageView *m1_imgArrow;

@property (nonatomic) UIView *m1_LineView;

@property (nonatomic) UIButton *m1_btnBackGround;

@property (nonatomic) M1RowPicker *m1_pickerP;

@end

@implementation M1RowPickView

@synthesize m1_paramDic = _m1_paramDic;

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubviews];
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initSubviews];
    }
    return self;
}

- (void)dealloc
{
    
}

#pragma mark - lazy load

- (M1RowPicker *)m1_pickerP
{
    if (!_m1_pickerP) {
        _m1_pickerP = [[M1RowPicker alloc] init];
    }
    return _m1_pickerP;
}

- (UIView *)m1_RowView
{
    if (!_m1_RowView) {
        _m1_RowView = [[UIView alloc] initWithFrame:CGRectMake(m1_pickerLeft, 0, 200 - m1_pickerLeft - m1_pickerRight, m1_pickerHeight)];
        [self addSubview:_m1_RowView];
    }
    return _m1_RowView;
}

- (UILabel *)m1_LBTitle
{
    if (!_m1_LBTitle) {
        _m1_LBTitle = [[UILabel alloc] init];
        _m1_LBTitle.backgroundColor = [UIColor clearColor];
        _m1_LBTitle.textAlignment = NSTextAlignmentLeft;
        _m1_LBTitle.textColor = [UIColor blackColor];
        _m1_LBTitle.font = [UIFont boldSystemFontOfSize:15];
    }
    return _m1_LBTitle;
}

- (M1PickerTF *)m1_pickerTextField
{
    if (!_m1_pickerTextField) {
        _m1_pickerTextField = [[M1PickerTF alloc] init];
        _m1_pickerTextField.textAlignment = NSTextAlignmentRight;
    }
    return _m1_pickerTextField;
}

- (UIImageView *)m1_imgHeader
{
    if (!_m1_imgHeader) {
        _m1_imgHeader = [[UIImageView alloc] init];
        _m1_imgHeader.contentMode = UIViewContentModeScaleAspectFill;
        _m1_imgHeader.layer.masksToBounds = YES;
        _m1_imgHeader.layer.cornerRadius = 35/2;
        _m1_imgHeader.userInteractionEnabled = YES;
    }
    return _m1_imgHeader;
}

- (UIImageView *)m1_imgArrow
{
    if (!_m1_imgArrow) {
        _m1_imgArrow = [[UIImageView alloc] initWithFrame:CGRectMake(0, (m1_pickerHeight - 12)/2, 20, 12)];
        _m1_imgArrow.contentMode = UIViewContentModeScaleAspectFit;
        _m1_imgArrow.image = [UIImage imageNamed:@"grey_s_arrow"];
    }
    return _m1_imgArrow;
}

- (UIView *)m1_LineView
{
    if (!_m1_LineView) {
        _m1_LineView = [[UIView alloc] init];
        _m1_LineView.backgroundColor = [UIColor lightGrayColor];
    }
    return _m1_LineView;
}

- (UIButton *)m1_btnBackGround
{
    if (!_m1_btnBackGround) {
        _m1_btnBackGround = [[UIButton alloc] initWithFrame:self.bounds];
        [_m1_btnBackGround addTarget:self action:@selector(m1_ClickBackGround) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_m1_btnBackGround];
    }
    return _m1_btnBackGround;
}

- (NSMutableDictionary *)m1_paramDic
{
    if (!_m1_paramDic) {
        _m1_paramDic = [NSMutableDictionary new];
        [_m1_paramDic setObject:@"" forKey:m1_RowPickerKeyName];
        [_m1_paramDic setObject:@"" forKey:m1_RowPickerKeyValue];
        [_m1_paramDic setObject:@[] forKey:m1_RowPickerKeyData];
        [_m1_paramDic setObject:@"" forKey:m1_RowPickerKeyHoldText];
        [_m1_paramDic setObject:@"" forKey:m1_RowPickerKeyDefaultSelect];
        [_m1_paramDic setObject:@(0) forKey:m1_RowPickerKeyKeyHasLine];
        [_m1_paramDic setObject:@(0) forKey:m1_RowPickerKeyHasArrow];
        [_m1_paramDic setObject:@(0) forKey:m1_RowPickerKeyHeight];
        [_m1_paramDic setObject:@(1) forKey:m1_RowPickerKeyPickNum];
        [_m1_paramDic setObject:@(0) forKey:m1_RowPickerKeyIsSelection];
        [_m1_paramDic setObject:@(1) forKey:m1_RowPickerKeyEditable];
        [_m1_paramDic setObject:@{
                               NSForegroundColorAttributeName:[UIColor blackColor],
                               NSFontAttributeName:[UIFont systemFontOfSize:16]
                               } forKey:m1_RowPickerKeyShowAttrs];
        [_m1_paramDic setObject:@{
                               NSForegroundColorAttributeName:[UIColor lightGrayColor],
                               NSFontAttributeName:[UIFont systemFontOfSize:16]
                               } forKey:m1_RowPickerKeyHoldAttrs];
    }
    return _m1_paramDic;
}

- (void)setM1_paramDic:(NSMutableDictionary *)paramDic
{
    for (NSString *key in paramDic.allKeys)
    {
        [self.m1_paramDic setObject:paramDic[key] forKey:key];
    }
}

#pragma mark - private method

- (void)initData
{
    m1_pickerHeight = [self.m1_paramDic[m1_RowPickerKeyHeight] floatValue] > 0 ? [self.m1_paramDic[m1_RowPickerKeyHeight] floatValue] : 70;
    m1_pickerLeft = self.m1_edgeInsetsRow.left > 0 ? self.m1_edgeInsetsRow.left : 0;
    m1_pickerRight = self.m1_edgeInsetsRow.right > 0 ? self.m1_edgeInsetsRow.right : 0;
    m1_pickerNum = [self.m1_paramDic[m1_RowPickerKeyPickNum] integerValue];
    
    m1_pickerName = self.m1_paramDic[m1_RowPickerKeyName];
    m1_pickerType = [self.m1_paramDic[m1_RowPickerKeyType] integerValue];
    m1_pickerValue = [NSString stringWithFormat:@"%@",self.m1_paramDic[m1_RowPickerKeyValue]];
    m1_pickerData = self.m1_paramDic[m1_RowPickerKeyData];
    m1_pickerHold = self.m1_paramDic[m1_RowPickerKeyHoldText];
    m1_pickerDefault = self.m1_paramDic[m1_RowPickerKeyDefaultSelect];
    m1_pickerHasLine = [self.m1_paramDic[m1_RowPickerKeyKeyHasLine] boolValue];
    m1_pickerHasArrow = [self.m1_paramDic[m1_RowPickerKeyHasArrow] boolValue];
    m1_pickerSelect = [self.m1_paramDic[m1_RowPickerKeyIsSelection] boolValue];
    m1_pickerEditable = [self.m1_paramDic[m1_RowPickerKeyEditable] boolValue];
    
    m1_pickerShowText = self.m1_paramDic[m1_RowPickerKeyShowAttrs];
    m1_pickerHoldText = self.m1_paramDic[m1_RowPickerKeyHoldAttrs];
    
    m1_isNew = YES;
}

- (void)initSubviews
{
    [self initData];
    
    [self.m1_RowView addSubview:self.m1_LBTitle];
    if (m1_isNew) {
        [self.m1_RowView addSubview:self.m1_pickerTextField];
        [self.m1_RowView addSubview:self.m1_imgHeader];
        [self.m1_RowView addSubview:self.m1_LineView];
    } else {
        [self.m1_RowView addSubview:self.m1_imgHeader];
        [self.m1_RowView addSubview:self.m1_LineView];
    }
    
    self.frame = CGRectMake(0, 0, 200, CGRectGetMaxY(self.m1_RowView.frame));
}


- (void)createPickViewNew
{
 
    self.m1_pickerTextField.hidden = NO;
    
    self.m1_btnBackGround.hidden = m1_pickerType == EnumPickType_textfield;
    
    self.m1_pickerTextField.delegate = self;
    
    if (m1_pickerType == EnumPickType_textfield)
    {
        [self.m1_pickerTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    }
    
    self.m1_pickerTextField.font = m1_pickerShowText[NSFontAttributeName];
    self.m1_pickerTextField.textColor = m1_pickerShowText[NSForegroundColorAttributeName];
    self.m1_pickerTextField.attributedPlaceholder = [self.m1_pickerP m1_attributedStr:m1_pickerHold
                                                                     selectText:m1_pickerHold
                                                                     selectAttr:m1_pickerHoldText];
    self.m1_pickerTextField.rightViewMode = UITextFieldViewModeAlways;
    if (m1_pickerHasArrow) {
        self.m1_pickerTextField.rightView = self.m1_imgArrow;
    } else {

        self.m1_pickerTextField.rightView = nil;
    }
    self.m1_pickerTextField.enabled = m1_pickerEditable;
    
    self.m1_pickerTextField.text = @"";

    
    self.m1_pickerTextField.m1_type = (EnumPickField)m1_pickerType;
    
    
}


#pragma mark public method

- (void)m1_updateData
{
    [self initData];
    
    self.m1_LBTitle.text = m1_pickerName;
    
    self.m1_LineView.hidden = !m1_pickerHasLine;
    
    CGFloat widthScreen = [UIScreen mainScreen].bounds.size.width;
    
    CGFloat widthLeft = [self.m1_LBTitle sizeThatFits:CGSizeMake(widthScreen, m1_pickerHeight)].width + 10;
    CGFloat widthRight = widthScreen - m1_pickerLeft - m1_pickerRight - widthLeft;
    
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, widthScreen, m1_pickerHeight);
    self.m1_RowView.frame = CGRectMake(m1_pickerLeft, 0, widthScreen - m1_pickerLeft - m1_pickerRight, m1_pickerHeight);
    self.m1_LBTitle.frame = CGRectMake(0, 0, widthLeft, m1_pickerHeight);
    
    if (m1_isNew) {
        self.m1_pickerTextField.frame = CGRectMake(widthLeft, 0, widthRight, m1_pickerHeight);
        self.m1_imgHeader.frame = CGRectMake(self.m1_RowView.frame.size.width - (m1_pickerHeight - 16) - 15, 8, m1_pickerHeight - 16 , m1_pickerHeight - 16);
        self.m1_imgHeader.layer.cornerRadius = self.m1_imgHeader.frame.size.height/2;
        self.m1_LineView.frame = CGRectMake(0, m1_pickerHeight - 1, self.m1_RowView.frame.size.width, 0.5);
        
        [self createPickViewNew];
    } else {

    }
    [self m1_updateNewView];
}

- (void)m1_updateValue:(NSString *)result value:(NSString *)value
{
    self.m1_pickerTextField.text = result;
    
    [self.m1_paramDic setObject:value forKey:m1_RowPickerKeyValue];
}

- (void)m1_updateNewView
{
    CGRect frame = self.m1_pickerTextField.frame;
    frame.size.width = frame.size.width - 50;
    frame.origin.x = frame.origin.x + 50;
    self.m1_pickerTextField.frame = frame;
    self.m1_pickerTextField.textAlignment = NSTextAlignmentLeft;
    
    self.m1_LBTitle.textColor = m1_color(0x333333);
    
    m1_pickerShowText = @{
                            NSForegroundColorAttributeName:m1_color(0x999999),
                            NSFontAttributeName:[UIFont systemFontOfSize:15]
                            };
    m1_pickerHoldText = @{
                            NSForegroundColorAttributeName:m1_color(0xcccccc),
                            NSFontAttributeName:[UIFont systemFontOfSize:15]
                            };
    self.m1_pickerTextField.font = m1_pickerShowText[NSFontAttributeName];
    self.m1_pickerTextField.textColor = m1_pickerShowText[NSForegroundColorAttributeName];
    self.m1_pickerTextField.attributedPlaceholder = [self.m1_pickerP m1_attributedStr:m1_pickerHold
                                                                              selectText:m1_pickerHold
                                                                              selectAttr:m1_pickerHoldText];
    self.m1_LineView.backgroundColor = m1_color(0xCCCCCC);
}

#pragma mark - UI

- (void)doImageClick
{

}

- (void)m1_ClickBackGround
{
    if (m1_pickerType == EnumPickType_button)
    {
        if (self.m1_resultBlock)
        {
            self.m1_resultBlock(self);
        }
    }
    else
    {
        [self.m1_pickerTextField m1_ClickBackGround];
    }
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    
    if (m1_pickerType == EnumPickType_button)
    {
        if (self.m1_resultBlock)
        {
            self.m1_resultBlock(self);
        }
        return NO;
    }
    else if (m1_pickerType == EnumPickType_textfield)
    {
        [self.m1_pickerTextField m1_addViewToWindow];
    }
    
    return YES;
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    return [self.m1_pickerP textField:textField shouldChangeCharactersInRange:range replacementString:string];
}

- (void)textFieldDidChange:(UITextField *)textField
{
    [self.m1_pickerP textFieldDidChange:textField];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (!m1_NULLString(m1_pickerValue) && [m1_pickerValue isEqualToString:textField.text])
    {
        return;
    }
    
    [self.m1_paramDic setObject:textField.text forKey:m1_RowPickerKeyValue];
    
    NSLog(@"key:%@,value:%@",m1_pickerName,textField.text);
    
    if (self.m1_resultBlock)
    {
        self.m1_resultBlock(self);
    }
}

@end
